package com.hutao.manage.controller;

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hutao.manage.constant.Constant;
import com.hutao.manage.enums.ExportDataEnum;
import com.hutao.manage.query.ExportCSVParam;
import com.hutao.manage.service.ExportDMService;
import com.hutao.manage.service.impl.ExportCSVServiceImpl;
import com.hutao.manage.util.CSVUtil;

@RequestMapping("/dm")
@Controller
public class ExportDMController {

	private Logger logger = LoggerFactory.getLogger(ExportDMController.class);
	
	@Autowired
	private ExportDMService exportDMService;
	
	@ResponseBody
	@RequestMapping("/exportDm")
	public void exportPatientinfo(HttpServletResponse response,
			HttpServletRequest request) {
		// 解决跨域问题
		response.setHeader("Access-Control-Allow-Origin", "*");
		try {
			
			String[] params = {
				"onlyId","mrId","inNumber","inTime","outTime","name","age","idcard","clinicDiagCode","clinicDiagName",
				"mainDiagCode","mainDiagName","otherDiagCode1","otherDiagCode2","otherDiagCode3","otherDiagCode4","otherDiagCode5",
				"otherDiagCode6","otherDiagCode7","otherDiagCode8","otherDiagCode9","otherDiagCode10","otherDiagName1","otherDiagName2","otherDiagName3",
				"otherDiagName4","otherDiagName5","otherDiagName6","otherDiagName7","otherDiagName8","otherDiagName9","otherDiagName10",
				"inspectTitle","inspectCode","inspectName","inspectResult","referenceValue","measurementUnit","inspectFlag"
			};
			
			String[] header = {
					"唯一标识","病案号","住院次数","入院时间","出院时间","姓名","年龄","身份证号","门诊诊断编码","门诊诊断名称",
					"主要诊断编码","主要诊断名称","其他诊断编码1","其他诊断编码2","其他诊断编码3","其他诊断编码4","其他诊断编码5",
					"其他诊断编码6","其他诊断编码7","其他诊断编码8","其他诊断编码9","其他诊断编码10","其他诊断名称1","其他诊断名称2","其他诊断名称3",
					"其他诊断名称4","其他诊断名称5","其他诊断名称6","其他诊断名称7","其他诊断名称8","其他诊断名称9","其他诊断名称10",
					"指标","检查项目代码","检查项目名称","检验结果","参考值","检查计量单位","标志"
				};
			
			exportDMRecordBatch(response,header,params);
		} catch (Exception e) {
			e.printStackTrace();

		}

	}
	
	
	/**
	 * 分批导入数据
	 * 
	 * @param response
	 * @param header
	 * @param fileName
	 * @throws IOException
	 */
	private void exportDMRecordBatch(HttpServletResponse response,String[] header,String[] params) throws IOException {
		response.setContentType("application/vnd.ms-excel;charset=GBK");
		response.setHeader("Content-Disposition",
				"attachment;filename=" + new String(("测过性激素的糖尿病患者相关信息").getBytes("GBK"), "ISO8859-1") + "." + "csv");
		response.setCharacterEncoding("UTF-8");
		StringBuilder sb = new StringBuilder();
		for (String s : header) {
			sb.append("\t").append(s).append(",");
		}
		sb.append("\n");
		OutputStream out = response.getOutputStream();
		try {
			out.write(new byte[] { (byte) 0xEF, (byte) 0xBB, (byte) 0xBF });
			out.write(sb.toString().getBytes());
			List<String> onlyIdList = exportDMService.getOnlyIdByDm();
			logger.info("总数量为:"+onlyIdList.size());
			for (String onlyId : onlyIdList) {
				List<Map<String,Object>> resultList = exportDMService.getMrInLabSpecimenByOnlyId(onlyId);
				if(CollectionUtils.isEmpty(resultList)) {
					continue;
				}
				
				int dataLength = resultList.size();
				for (int i = 0; i < dataLength; i++) {
					// 解决乱码
					out.write(new byte[] { (byte) 0xEF, (byte) 0xBB, (byte) 0xBF });
					out.write(CSVUtil.handleExportData(resultList.get(i), params).getBytes("UTF-8"));
				}
				out.flush();
			}
			
		
			
			

		} catch (IOException e) {
			e.printStackTrace();
			
		} finally {
			if (out != null) {
				out.close();
			}
		}
	}


	
}
